package com.huifaapp.mall.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.AfterStatusBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

/**
 * Created by Alienware on 2018/1/16.
 */

public class AfterStatusActivity extends BaseActivity {
    TextView statusText, statusTitleText,shop_name;
    View waitLayout, wuliuLayout;
    View returnAllLayout, returnBackCountLayout, addressLayout, nagativiDesLayout;
    View editAddressBtn;
    String  aftersaleid;
    ImageView shop_image;
    TextView tv_tkreason;
    TextView tv_tkmoney;
    TextView tv_sqcount;
    TextView tv_sqtime;
    TextView tv_tknum;
    TextView shop_xj,shop_yj,shop_num;
    TextView shop_param;

    Button connectMjBtn, cancelBtn, changeBtn;
    TextView tv_tkzmoney,tv_tkyzh;
    /**
     * 该实体类我直接写好了  到时候获取数据的时候 直接用这个类解析数据
     */
//    AfterStatusInfo afterStatusInfo;
    AfterStatusBean afterStatusBean;
    //    AfterStatusInfo.Data data;
//    AfterStatusInfo.GoodsDetailVo goodsDetailVo;
//    AfterStatusInfo.Param param;
    AfterStatusBean.DataBean dataBean;
    AfterStatusBean.DataBean.GoodsDetailVoBean goodsDetailVoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_after_status;
    }

    @Override
    protected void initialize() {


        //这里面是我写的测试数据  直接解析就可以了
//        afterStatusInfo = new AfterStatusInfo();
//        afterStatusInfo.setCode(10000);
//        afterStatusInfo.setMsg("请求成功！");
//        AfterStatusInfo.Data data = new AfterStatusInfo.Data();
//        data.setState(1);
//        data.setContent("您的退款正在审核中，收到商品不影响第二次销售，我们将确认收货");
//        data.setRefundAmount(0);
//        data.setReason("与描述不符");
//        data.setApplycount(1);
//        data.setApplyTime("2017-12-26 15:41:19");
//        data.setReturnGoodsNo("156489431686");
//        data.setExpireTime(589425);
//        final AfterStatusInfo.GoodsDetailVo goodsDetailVo = new AfterStatusInfo.GoodsDetailVo();
//        goodsDetailVo.setGoodsImg("http://file.jhjvip.cn/images/default.jpg");
//        goodsDetailVo.setCount(1);
//        goodsDetailVo.setGoodsName("桃李面包");
//        goodsDetailVo.setPrice(36.6f);
//        goodsDetailVo.setOrginPrice(18.8f);
//        AfterStatusInfo.Param param = new AfterStatusInfo.Param();
//        param.setColor("white");
//        param.setBrand("太大");
//        param.setSize("xxl");
//        goodsDetailVo.setParam(param);
//        data.setGoodsDetailVo(goodsDetailVo);
//        afterStatusInfo.setData(data);

        Intent intent = getIntent();
        aftersaleid = intent.getStringExtra("aftersaleId");
        Log.e("SHOUHOUQIANGXING",aftersaleid);

        Log.e("售后详情url---------",ApiManager.BASE_NIUPAI_URL+"orders/getapplyDetail?aftersaleId="+aftersaleid);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/getapplyDetail")
                .addParams("aftersaleId",aftersaleid)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        ToastUtils.showShort("获取失败");
                    }

                    @Override
                    public void onResponse(String response) {
                        try{
                            Gson gson = new Gson();
                            afterStatusBean = gson.fromJson(response,AfterStatusBean.class);
                            dataBean = afterStatusBean.getData();
                            goodsDetailVoBean = dataBean.getGoodsDetailVo();

                            statusTitleText.setText(dataBean.getContent());
                            tv_tkreason.setText("退款原因:"+" "+dataBean.getReason());
                            tv_tkmoney.setText("退款金额:"+" ¥"+dataBean.getApplycount());
                            tv_sqcount.setText("申请件数:"+" "+dataBean.getApplycount()+"");
                            tv_sqtime.setText("申请时间:"+" "+dataBean.getApplyTime()+"");
                            tv_tknum.setText("退款编号:"+" "+dataBean.getReturnGoodsNo()+"");
                            shop_name.setText(goodsDetailVoBean.getGoodsName());
                            Glide.with(AfterStatusActivity.this).load(goodsDetailVoBean.getGoodsImg()).into(shop_image);
                            shop_xj.setText("¥  "+goodsDetailVoBean.getPrice()+"");
                            shop_yj.setText("¥  "+goodsDetailVoBean.getOrginPrice()+"");
                            shop_num.setText("X"+goodsDetailVoBean.getCount()+"");
                            String color = goodsDetailVoBean.getParam().getColor();
                            //String size = goodsDetailVoBean.getParam().
                            shop_param.setText("颜色分类: "+color+";"+"尺寸:xxxxx");
                            tv_tkyzh.setText("¥"+dataBean.getRefundAmount()+"");
                            tv_tkzmoney.setText("¥"+dataBean.getRefundAmount()+"");

                            initData();
                            initListener();
                        }catch (Exception e){
                            e.printStackTrace();
                            ToastUtils.showShort("SHIBAI");
                        }


                    }
                });

    }

    @Override
    protected void initView() {
        statusText = (TextView) findViewById(R.id.statusText);
        statusTitleText = (TextView) findViewById(R.id.status_TitleText);
        waitLayout = findViewById(R.id.waitLayout);
        wuliuLayout = findViewById(R.id.wuliuLayout);
        returnAllLayout = findViewById(R.id.returnAllLayout);
        returnBackCountLayout = findViewById(R.id.returnBackCountLayout);
        addressLayout = findViewById(R.id.addressLayout);
        nagativiDesLayout = findViewById(R.id.nagativiDesLayout);
        editAddressBtn = findViewById(R.id.editAddressBtn);
        shop_image = (ImageView) findViewById(R.id.status_shopImage);
        shop_name = (TextView) findViewById(R.id.stastus_shopname);
        tv_tkmoney = (TextView) findViewById(R.id.status_tkmoney);
        tv_sqcount = (TextView) findViewById(R.id.status_sqcount);
        tv_sqtime = (TextView) findViewById(R.id.status_sqtime);
        tv_tknum = (TextView) findViewById(R.id.status_tknum);
        shop_xj = (TextView) findViewById(R.id.status_shopxj);
        shop_yj = (TextView) findViewById(R.id.status_shopyj);
        shop_num = (TextView) findViewById(R.id.status_shopnum);
        shop_param = (TextView) findViewById(R.id.status_param);
        tv_tkzmoney = (TextView) findViewById(R.id.status_tkzmoney);
        tv_tkyzh = (TextView) findViewById(R.id.status_tkyzh);


        connectMjBtn = (Button) findViewById(R.id.connectMjBtn);
        cancelBtn = (Button) findViewById(R.id.cancelBtn);
//        changeBtn = (Button) findViewById(R.id.changeBtn);
        tv_tkreason = (TextView) findViewById(R.id.status_tkreason);


    }

    /**
     * 初始化数据
     */
    private void initData(){


        switch (dataBean.getState()+"") {
            case "001":
                //退款中
                statusText.setText("退款中");
                connectMjBtn.setVisibility(View.VISIBLE);
                cancelBtn.setVisibility(View.GONE);
//                changeBtn.setVisibility(View.GONE);
                returnAllLayout.setVisibility(View.VISIBLE);
                break;
            case "002":
                //换货关闭/退货关闭
                statusText.setText("换货关闭/退款退货关闭");
                connectMjBtn.setVisibility(View.VISIBLE);
                cancelBtn.setVisibility(View.GONE);
//                changeBtn.setVisibility(View.GONE);
                break;
            case "003":
                //待处理
                statusText.setText("等待售后处理");
                waitLayout.setVisibility(View.VISIBLE);
                connectMjBtn.setVisibility(View.VISIBLE);
                cancelBtn.setVisibility(View.VISIBLE);
//                changeBtn.setVisibility(View.VISIBLE);
                break;
            case "004":
                //售后拒绝
                statusText.setText("售后已拒绝，您需要及时响应");
                connectMjBtn.setVisibility(View.VISIBLE);
                cancelBtn.setVisibility(View.VISIBLE);
//                changeBtn.setVisibility(View.VISIBLE);

                nagativiDesLayout.setVisibility(View.VISIBLE);
                break;
            case "005":
                //同意换货填写物流
                statusText.setText("请退货并填写物流信息");
                connectMjBtn.setVisibility(View.VISIBLE);
                cancelBtn.setVisibility(View.VISIBLE);
//                changeBtn.setVisibility(View.GONE);

                wuliuLayout.setVisibility(View.VISIBLE);
                waitLayout.setVisibility(View.VISIBLE);
                addressLayout.setVisibility(View.VISIBLE);
                break;
            case "006":
                //退款成功
                statusText.setText("退款成功");
                connectMjBtn.setVisibility(View.VISIBLE);
                cancelBtn.setVisibility(View.GONE);
//                changeBtn.setVisibility(View.GONE);
                returnAllLayout.setVisibility(View.VISIBLE);
                returnBackCountLayout.setVisibility(View.VISIBLE);
                break;

        }
//        if (dataBean.getState().equals("001")){
//             // 退款中
//                statusText.setText("退款中");
//                connectMjBtn.setVisibility(View.VISIBLE);
//                cancelBtn.setVisibility(View.GONE);
//                changeBtn.setVisibility(View.GONE);
//                returnAllLayout.setVisibility(View.VISIBLE);
//        }else if (dataBean.getState().equals("002")){
        //换货关闭/退货关闭
//                statusText.setText("换货关闭/退款退货关闭");
//                connectMjBtn.setVisibility(View.VISIBLE);
//                cancelBtn.setVisibility(View.GONE);
//                changeBtn.setVisibility(View.GONE);


    }
    //statusTitleText.setText(afterStatusInfo.getData().getContent());


    private void initListener(){
        connectMjBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击联系卖家按钮
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击撤销申请

                AlertDialog alertDialog = new AlertDialog.Builder(AfterStatusActivity.this).setTitle("提示").setMessage("撤销后，若订单超出服务保障期，则无法再发起售后申请请求！？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"orders/delApplyDetail")
                                        .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                        .addParams("appId", AUniqueIdentifier.getDeviceId(AfterStatusActivity.this))
                                        .addParams("afterSafeId",aftersaleid)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {

                                            }
                                            @Override
                                            public void onResponse(String response) {
                                                try {
                                                    JSONObject jsonObject = new JSONObject(response);
                                                    String code = jsonObject.getString("code");
                                                    if (code.equals("10000")){ //请求成功！
                                                        ToastUtils.showShort("成功");

                                                    }else {
                                                        ToastUtils.showShort("失败");
                                                    }
                                                }catch (Exception e){
                                                    e.printStackTrace();
                                                }

                                            }
                                        });


                            }
                        }).create();
                alertDialog.show();

            }
        });

//        changeBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //点击修改申请
//            }
//        });

        editAddressBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击填写物流单号
                // TODO: 2018/1/16 跳转至填写物流信息界面
                Intent intent = new Intent(AfterStatusActivity.this, AfterlogisticsActivity.class);
                intent.putExtra("aftersaleId",aftersaleid);
                startActivity(intent);
            }
        });
    }
}
